/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usart.h"
#include "semphr.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for Task_H */
osThreadId_t Task_HHandle;
const osThreadAttr_t Task_H_attributes = {
  .name = "Task_H",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityHigh,
};
/* Definitions for Task_M */
osThreadId_t Task_MHandle;
const osThreadAttr_t Task_M_attributes = {
  .name = "Task_M",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Task_L */
osThreadId_t Task_LHandle;
const osThreadAttr_t Task_L_attributes = {
  .name = "Task_L",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityBelowNormal,
};
/* Definitions for token */
osMutexId_t tokenHandle;
const osMutexAttr_t token_attributes = {
  .name = "token"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void AppTask_H(void *argument);
void AppTask_M(void *argument);
void AppTask_L(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */
  /* Create the mutex(es) */
  /* creation of token */
  tokenHandle = osMutexNew(&token_attributes);

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of Task_H */
  Task_HHandle = osThreadNew(AppTask_H, NULL, &Task_H_attributes);

  /* creation of Task_M */
  Task_MHandle = osThreadNew(AppTask_M, NULL, &Task_M_attributes);

  /* creation of Task_L */
  Task_LHandle = osThreadNew(AppTask_L, NULL, &Task_L_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_AppTask_H */
/**
  * @brief  Function implementing the Task_H thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_AppTask_H */
void AppTask_H(void *argument)
{
  /* USER CODE BEGIN AppTask_H */
	uint8_t strHigh[]="Task_H get token\n";
  /* Infinite loop */
  for(;;)
  {
    if(xSemaphoreTake(tokenHandle,portMAX_DELAY)==pdTRUE)
    {
    	HAL_UART_Transmit(&huart1, strHigh, sizeof(strHigh), 300);
    	xSemaphoreGive(tokenHandle);
    }
    vTaskDelay(500);
  }
  /* USER CODE END AppTask_H */
}

/* USER CODE BEGIN Header_AppTask_M */
/**
* @brief Function implementing the Task_M thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_AppTask_M */
void AppTask_M(void *argument)
{
  /* USER CODE BEGIN AppTask_M */
	uint8_t strMid[]="Task_M is running\n";
  /* Infinite loop */
  for(;;)
  {
	HAL_UART_Transmit(&huart1, strMid, sizeof(strMid), 300);
	vTaskDelay(500);
  }
  /* USER CODE END AppTask_M */
}

/* USER CODE BEGIN Header_AppTask_L */
/**
* @brief Function implementing the Task_L thread.
* @param argument: Not used
* @retval None
*/

/* USER CODE END Header_AppTask_L */
void AppTask_L(void *argument)
{
  /* USER CODE BEGIN AppTask_L */
	uint8_t str1[]="Task_LOW take it\n";
	uint8_t str2[]="Task_LOW give it\n";
  /* Infinite loop */
  for(;;)
  {
	  if(xSemaphoreTake(tokenHandle,pdMS_TO_TICKS(200))==pdTRUE)
	      {
	      	HAL_UART_Transmit(&huart1, str1, sizeof(str1), 300);
	      	HAL_Delay(1000);
	      	HAL_UART_Transmit(&huart1, str2, sizeof(str2), 300);
	      	HAL_Delay(10);
	      	xSemaphoreGive(tokenHandle);
	      }
	  vTaskDelay(20);
  }
  /* USER CODE END AppTask_L */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

